/**
 * 
 */
package com.disys.admin.security;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;

import javax.faces.context.FacesContext;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.PagedResultsControl;

/**
 * @author Ubaid Raja
 * 
 */

public class LDAPAuth {

	public Hashtable<String, String> env = null;

	public Control[] connCtls = null;
	Context ctx;
	DirContext dirContext;
	public static LdapContext ldapContext = null;
	String baseName = "uid=admin,ou=system,dc=elixir,dc=com";
	String modelUsername = "template";
	String serverIP = "localhost";

	public LDAPAuth() {

	}

	public static void main(String[] args) {
		LDAPAuth ldapContxCrtn = new LDAPAuth();
		ldapContext = ldapContxCrtn.getLdapContext("admin", "secret");
		if (ldapContext != null) {
			ldapContxCrtn.closeConnection(ldapContext);
		}

	}

	public LdapContext getLdapContext(String username, String password) {
		try {
			Properties prop = new Properties();
			ClassLoader loader = Thread.currentThread()
					.getContextClassLoader();
			InputStream stream = loader
					.getResourceAsStream("ldap.properties");
			prop.load(stream);

						
			Hashtable<String, String> env = new Hashtable<String, String>();

			env.put(Context.INITIAL_CONTEXT_FACTORY,
					prop.getProperty("INITIAL_CONTEXT_FACTORY"));
			env.put(Context.REFERRAL, prop.getProperty("REFERRAL"));
			env.put(Context.SECURITY_AUTHENTICATION, prop.getProperty("SECURITY_AUTHENTICATION"));
			env.put(Context.PROVIDER_URL, prop.getProperty("PROVIDER_URL"));
			String baseDn = "cn="+username+","+prop.getProperty("BASE_DN_USERS");
			env.put(Context.SECURITY_PRINCIPAL, baseDn);
			env.put(Context.SECURITY_CREDENTIALS, password);
			LdapContext ctx = null;
			try {
				ctx = new InitialLdapContext(env, null);
//				ctx.setRequestControls(new Control[] { new PagedResultsControl(
//						10000, Control.CRITICAL) });
			}catch(AuthenticationException ae){
				baseDn = "uid="+username+","+prop.getProperty("BASE_DN_ADMIN");
				env.put(Context.SECURITY_PRINCIPAL, baseDn);
				try {
					ctx = new InitialLdapContext(env, null);
				} catch (NamingException e) {
					e.printStackTrace();
				}	
			}
			catch (NamingException e) {
				e.printStackTrace();
			}
			
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("baseDn", baseDn);
			return ctx;
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return null;		
	}

	public void closeConnection(LdapContext ldapContext) {
		try {
			ldapContext.close();
			System.out.println("Context is closed");
		} catch (NamingException e) {
			System.out.println("Context close failure " + e);
		}
	}

}
